home *** CD-ROM | disk | FTP | other *** search
- package sub_arctic.constraints;
-
- import sub_arctic.lib.manager;
- import sub_arctic.lib.sub_arctic_error;
-
- /**
- * An external (heavyweight) constraint for computing a linear value
- * (A*x + B for two fixed values A and B and one variable x). This class
- * provides an example of how to do a stand-alone external constraint.
- */
- public class linear_ext_constraint extends external_constraint {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** "A" coefficient in A*x+B equation. */
- protected int _A;
-
- /** "A" coefficient in A*x+B equation. */
- public int A() {return _A;}
-
- /** Set the "A" coefficient in A*x+B equation. */
- public void set_A(int v) {_A = v;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** "B" coefficient in A*x+B equation. */
- protected int _B;
-
- /** "B" coefficient in A*x+B equation. */
- public int B() {return _B;}
-
- /** Set the "B" coefficient in A*x+B equation. */
- public void set_B(int v) {_B = v;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Provider part that gives the value we compute from */
- protected provider_part_ref _target;
-
- /** Provider part that gives the value we compute from */
- public provider_part_ref target() {return _target;}
-
- /** Set the provider part that gives the value we compute from */
- public void set_target(provider_part_ref targ)
- {
- /* detach from any old target */
- if (_target != null && _target.obj != null)
- _target.obj.detach_dependent(_target.part_num, this,0);
-
- /* establish new target and attach to that */
- _target = targ;
- if (_target != null && _target.obj != null)
- _target.obj.attach_dependent(_target.part_num, this,0);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Constructor.
- * @param int a the A value in A*x+B.
- * @param int b the B value in A*x+B.
- * @param value_provider v_obj the object we obtain the x value from in
- * A*x+B.
- * @param int v_part the part number of the above object that we
- * obtain the x value from in A*x+B.
- */
- public linear_ext_constraint(int a, int b, value_provider v_obj, int v_part)
- {
- /* initialize */
- _A = a;
- _B = b;
- _target = new provider_part_ref(v_obj, v_part);
-
- /* attach to our target */
- if (_target != null && _target.obj != null)
- _target.obj.attach_dependent(_target.part_num, this,0);
- }
-
- //had:
- //*@exception bad_value if an invalid value_provider or part number is given.
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Finalize by cutting off from any target */
- public void finalize()
- {
- set_target(null);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Compute our value as A*x+B where is x is an integer obtained from
- * our target provider part. Here part_number is ignored since we only
- * provide one value. <p>
- *
- * @param int part_number the part number of the value being requested from
- * us. Since we have only one part we ignore this.
- * @return Object an Integer value providing the result.
- */
- public Object get_value(int part_number)
- {
- Object raw_v;
-
- /* if we have no target, we're in trouble */
- if (target() == null || target().obj == null)
- throw new sub_arctic_error("Null target object in linear_ext_constraint");
-
- /* get the value and type check */
- raw_v = target().obj.get_value(target().part_num);
- if (!(raw_v instanceof Integer))
- throw new sub_arctic_error("Value is not Integer in linear_ext_constraint");
-
- /* do the computation */
- return new Integer(A() * (((Integer)raw_v).intValue()) + B());
-
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- }
-
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-